<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Api\ApiController;
use App\Http\Forms\BannerForm;
use App\Http\Forms\PageConfigForm;
use App\Http\Forms\RebateRulesForm;
use App\Http\Forms\RecommendForm;
use App\Http\Forms\SystemSwitchForm;
use App\Modules\Common\Models\PageConfig;
use App\Modules\Common\Models\SystemSwitch;
use App\Modules\Shop\Models\Banner;
use App\Modules\Shop\Shop;
use App\Modules\System\Recommend;
use App\Modules\System\RuleIntroduce;
use Illuminate\Http\Request;

class SystemController extends ApiController
{
    public function bannerList()
    {
        $this->authorize('system');
        $data = Banner::query()
            ->where('type', Banner::TYPE_PLATFORM)
            ->get();
        $sidebar = 'banner';
        return view('system.banner', compact('data', 'sidebar'));
    }

    public function createBanner()
    {
        $this->authorize('system-edit');
        $shop = Shop::query()
            ->get()
            ->pluck('name', 'id')
            ->all();

        $form = $this->form( BannerForm::class, [
            'method' => 'PUT',
            'url' => route('admin.system.banner.store'),
        ], [
            'shop' => $shop,
        ]);
        $sidebar = 'banner';
        $flag = 'banner';
        return view('system.form', compact('form', 'sidebar', 'flag'));
    }

    public function storeBanner()
    {
        $this->authorize('system-edit');
        $form = $this->form(BannerForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $data['type'] = Banner::TYPE_PLATFORM;
        $data['sort']  = $data['sort']??0;
        if ($data['aim'] == 'url' && empty($data['url'])) {
            return redirect()->back()->withErrors("地址字段不能为空")->withInput();
        }
        if ($data['aim'] != 'url' && empty($data['link_id'])) {
            return redirect()->back()->withErrors("商品不能为空")->withInput();
        }
        Banner::query()->create($data);
        return redirect()->route('admin.system.banner');
    }

    public function delBanner($id)
    {
        $this->authorize('system-edit');
        Banner::query()->where('id', $id)->delete();
        return redirect()->route('admin.system.banner');
    }

    public function recommend()
    {
        $this->authorize('system-hot');
        $data = Recommend::query()
            ->limit(4)
            ->get();
        $sidebar = 'recommend';
        return view('system.recommend', compact('data', 'sidebar'));
    }

    public function editRecommend($id)
    {
        $this->authorize('system-hot');
        $recommend = Recommend::query()->find($id);
        $form = $this->form(RecommendForm::class, [
            'method' => 'POST',
            'url' => route('admin.system.recommend.update', ['id' => $id]),
            'model' => $recommend,
        ]);
        $sidebar = 'recommend';
        return view('system.form', compact('form', 'sidebar'));
    }

    public function updateRecommend($id)
    {
        $this->authorize('system-hot');
        $form = $this->form(RecommendForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        Recommend::query()
            ->updateOrCreate([
                'id' => $id,
            ], $data);
        return redirect()->route('admin.system.recommend');
    }

    public function editPageConfigs()
    {
        $this->authorize('system-page');
        $config = PageConfig::query()->first();
        $form = $this->form(PageConfigForm::class, [
            'method' => 'post',
            'url' => route('admin.system.page.update'),
            'model' => $config,
        ]);
        $sidebar = 'page';
        $flag = 'page';
        return view('system.form', compact('form', 'sidebar', 'flag'));
    }

    public function updatePageConfigs()
    {
        $this->authorize('system-page');
        $form = $this->form(PageConfigForm::class);

        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $data['can_apply_partner'] = $data['can_apply_partner']??0;
        PageConfig::query()
            ->updateOrCreate([
                'id' => 1
            ], $data);
        return redirect()->route('admin.system.page');
    }

    public function Rank()
    {
        $this->authorize('system-rank');
        $switch = SystemSwitch::query()->first();
        $form = $this->form(SystemSwitchForm::class, [
            'method' => 'post',
            'url' => route('admin.system.rank.update'),
            'model' => $switch,
        ]);
        $sidebar = 'rank';
        return view('system.form', compact('form', 'sidebar'));
    }

    public function updateRank()
    {
        $this->authorize('system-rank');
        $form = $this->form(SystemSwitchForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        SystemSwitch::query()
            ->updateOrCreate([
                'id' => 1,
            ], [
                'rebate_ranking' => $data['rebate_ranking']??0,
                'money_ranking' => $data['money_ranking']??0,
                'number_ranking' => $data['number_ranking']??0,
                'photo_ranking' => $data['photo_ranking']??0,
                'jewelry_ranking' => $data['jewelry_ranking']??0,
                'partner_rebate_ranking' => $data['partner_rebate_ranking']??0,
                'partner_money_ranking' => $data['partner_money_ranking']??0,
                'partner_number_ranking' => $data['partner_number_ranking']??0,
                'partner_photo_ranking' => $data['partner_photo_ranking']??0,
                'partner_jewelry_ranking' => $data['partner_jewelry_ranking']??0,
            ]);
        return redirect()->back();
    }

    public function ruleList()
    {
        $this->authorize('system-rule');
        $data = RuleIntroduce::query()
            ->orderBy('sort', 'desc')
            ->paginate(20);
        $sidebar = 'rule';
        return view('system.rule', compact('data', 'sidebar'));
    }

    public function createRule()
    {
        $this->authorize('system-rule');
        $form = $this->form(RebateRulesForm::class, [
            'method' => 'PUT',
            'url' => route('admin.system.rule.store'),
        ]);
        $sidebar = 'rule';
        return view('system.form', compact('form', 'sidebar'));
    }

    public function storeRule(Request $request)
    {
        $this->authorize('system-rule');
        $form = $this->form(RebateRulesForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();

        RuleIntroduce::query()
            ->create([
                'image' => $data['image'],
            ]);
        return redirect()->route('admin.system.rule');
    }

    public function editRule($id)
    {
        $this->authorize('system-rule');
        $rule = RuleIntroduce::query()->find($id);

        $form = $this->form(RebateRulesForm::class, [
            'method' => 'POST',
            'url' => route('admin.system.rule.update',['id' => $id]),
            'model' => $rule,
        ]);
        $sidebar = 'rule';
        return view('system.form', compact('form', 'sidebar'));
    }

    public function updateRule($id, Request $request)
    {
        $this->authorize('system-rule');
        $form = $this->form(RebateRulesForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        RuleIntroduce::query()
            ->where('id', $id)
            ->update([
                'image' => $data['image'],
            ]);
        return redirect()->route('admin.system.rule');
    }

    public function delRule($id)
    {
        $this->authorize('system-rule');
        RuleIntroduce::query()
            ->where('id', $id)
            ->delete();
        return redirect()->back();

    }

    public function changeRuleSort(Request $request)
    {
        $this->authorize('system-rule');
        $id = $request->input('id');
        $sort = $request->input('sort');
        RuleIntroduce::query()
            ->where('id', $id)
            ->update(['sort' => $sort]);
        return null;
    }
}
